package com.example.week04_work2;

/**
 * @author nhsoft.ll
 */
public class WaitDemo {


    public static void main(String[] args) throws Exception {
        WaitDemoSumTask sumTask = new WaitDemoSumTask();

        long start = System.currentTimeMillis();
        sumTask.start();
        synchronized (sumTask) {
            sumTask.wait();
        }
        System.out.println("异步计算结果为：" + sumTask.getResult());
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

    }
}
class WaitDemoSumTask extends Thread{

    private int result;

    public int getResult() {
        return result;
    }

    @Override
    public void run() {
        synchronized(this){
            result = sum();
            System.out.println("线程运行结果:" + result);
            //notify();
            notifyAll();
        }

    }

    private int sum() {
        return fibo(36);
    }

    private int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}


